﻿using System;

namespace Borg
{
    [Serializable]
    public abstract class RuntimeElement : MarshalByRefObject
    {
        private readonly Runtime _runtime;

        public Runtime Runtime
        {
            get { return _runtime; }
        }

        public IDispatcher Dispatcher
        {
            get { return _runtime.Dispatcher; }
        }

        protected Activator Activator
        {
            get { return _runtime.Activator; }
        }

        protected Loader Loader
        {
            get { return _runtime.Loader; }
        }

        protected TypeStore TypeStore
        {
            get { return _runtime.TypeStore; }
        }

        protected TypeStore.Modifier TypeStoreModifier
        {
            get { return _runtime.TypeStoreModifier; }
        }

        protected Composer Composer
        {
            get { return _runtime.Composer; }
        }

        protected CompositionEngine Engine
        {
            get { return _runtime.Engine; }
        }

        protected GarbageCollector GC
        {
            get { return _runtime.GC; }
        }

        protected InstanceStore InstanceStore
        {
            get { return _runtime.InstanceStore; }
        }

        protected InstanceStore.Modifier InstanceStoreModifier
        {
            get { return _runtime.InstanceStoreModifier; }
        }

        protected Qualifier Qualifier
        {
            get { return _runtime.Qualifier; }
        }

        protected Notifier Notifier
        {
            get { return _runtime.Notifier; }
        }

        protected ExtensionMethodInvoker Invoker
        {
            get { return _runtime.Invoker; }
        }

        protected Logger Logger
        {
            get { return _runtime.Logger; }
        }

        protected Persistor Persistor
        {
            get { return _runtime.Persistor; }
        }

        protected SettingStore SettingStore
        {
            get { return _runtime.SettingStore; }
        }

        protected RuntimeElement(Runtime runtime)
        {
            _runtime = runtime;
        }

        protected internal bool CheckAccess()
        {
            return Dispatcher.CheckAccess();
        }

        protected internal static Exception GetAccessException()
        {
            return new InvalidOperationException(
                "The calling thread cannot access the runtime because a different thread owns it.");
        }
    }
}
